<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>菜单编辑</title>
    <link rel="stylesheet" href="../static/plugins/layui/css/layui.css">

    <link rel="stylesheet" href="../static/plugins/zTree3/css/zTreeStyle/zTreeStyle.css">
    <link rel="stylesheet" href="../static/plugins/zTree3/css/demo.css">

</head>
<style>
    .ok-body {
        padding: 10px
    }
</style>

<body>
<div class="ok-body">

    <blockquote class="layui-elem-quote layui-text">
        菜单管理
    </blockquote>

    <div class="layui-container">
        <div class="layui-row">
            <div class="layui-col-md3">
                <ul id="tree" class="ztree"></ul>
            </div>
            <div class="layui-col-md7 layui-col-md-offset1">
                <form class="layui-form" action="">
                    <div class="layui-form-item">
                        <input type="hidden" name="id" id="id" th:value="${id}">
                        <label class="layui-form-label">名称</label>
                        <div class="layui-input-inline">
                            <input type="text" name="name" lay-verify="required" autocomplete="off" class="layui-input">
                        </div>
                        <div class="layui-form-mid layui-word-aux">必填</div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">URL</label>
                        <div class="layui-input-inline">
                            <input type="text" name="url" lay-verify="required" autocomplete="off" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">父节点</label>
                        <input type="hidden" name="parentId" lay-verify="required">
                        <div class="layui-input-inline">
                            <input type="text" name="parentName" lay-verify="required" readonly autocomplete="off"
                                   class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">描述</label>
                        <div class="layui-input-inline">
                            <input type="text" name="description" lay-verify="required" autocomplete="off"
                                   class="layui-input">
                        </div>
                    </div>


                    <div class="layui-form-item">
                        <label class="layui-form-label">类型</label>
                        <div class="layui-input-inline">
                            <select name="type" lay-verify="required" lay-filter="menuType">
                                <option value=""></option>
                                <option value="0">导航</option>
                                <option value="1">菜单</option>
                                <option value="2">功能</option>
                            </select>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">排序</label>
                        <div class="layui-input-inline">
                            <input type="text" name="orderby" lay-verify="number" maxlength="2" autocomplete="off"
                                   class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">状态</label>
                        <div class="layui-input-block">
                            <input type="checkbox" checked="" name="status" lay-skin="switch" value="1"
                                   lay-filter="switchTest" lay-text="启用|禁用">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button class="layui-btn" lay-submit="" lay-filter="submit">立即提交</button>
                            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        </div>
                    </div>

                </form>
            </div>
        </div>
    </div>

</div>

<script src="../static/plugins/layui/layui.js"></script>
<script src="../static/js/jquery-3.2.1.min.js"></script>
<script src="../static/js/jquery.base64.js"></script>
<script src="../static/js/device.js"></script>

<script src="../static/plugins/zTree3/js/jquery.ztree.core.min.js"></script>
<script src="../static/plugins/zTree3/js/jquery.ztree.exhide.min.js"></script>

<script th:inline="none">
    layui.use(['form', 'layer'], function () {
        var form = layui.form;
        var layer = layui.layer;
        var tableName = 'menu';
        var id = $('#id').val();
        var zTreeObj;
        var PARENT_ID;
        var setting = {
            treeId: 'tree',
            callback: {
                onClick: function (event, treeId, treeNode) {
                    // var level = treeNode.level;
                    // console.log(level);
                    var menuType = JJZ.named('type').val();
                    if (menuType === '0') {
                        JJZ.named('parentId').val(0);
                        JJZ.named('parentName').val('无');
                    } else {
                        JJZ.named('parentId').val(treeNode.id);
                        JJZ.named('parentName').val(treeNode.name);
                    }
                }
            },
            data: {
                simpleData: {
                    enable: true,
                    idKey: "id",
                    pIdKey: 'parentId',
                    rootPid: 0
                }
            }
        };

        InitTree();


        form.on('select(menuType)', function (data) {
            if (data.value == 0) {
                JJZ.named('parentId').val(0);
                JJZ.named('parentName').val('无');
            }

        });

        if (id) {
            //表示是修改页面
            var param = 'current=1&pageSize=2&searchCol=id&searchValue=' + id;
            JJZ.requestTableData(tableName, param, function (data) {
                var result = data.records[0];
                JJZ.named('name').val(result.name);
                JJZ.named('description').val(result.description);
                JJZ.named('url').val(result.url);
                JJZ.named('orderby').val(result.orderby);
                JJZ.named('parentId').val(result.parentId);
                PARENT_ID = result.parentId;
                JJZ.named('type').find('option').each(function (index, elem) {
                    $(elem).val() == result.type ? $(elem).attr("selected", "selected") : '';
                });

                var treeObj = $.fn.zTree.getZTreeObj("tree");
                //树存在  即先构造完树结构
                if (treeObj !== null) {
                    var node = treeObj.getNodeByParam("id", PARENT_ID);
                    if(node!==null){
                        JJZ.named('parentName').val(node.name);
                    }else{
                        JJZ.named('parentName').val('无');
                    }
                }

                result.status == 1 ? '' : JJZ.named('status').removeAttr("checked");

                form.render();
            });
        } else {
            //表示是新增页面
            JJZ.named('parentId').val(0);
            JJZ.named('parentName').val('无');
        }

        function InitTree() {
            JJZ.requestTableData('menu', '', function (data) {
                var treeNodes = data.records;
                var zNodes = [];
                for (var index in treeNodes) {
                    if (treeNodes[index].type != 2) {
                        var obj = {
                            id: treeNodes[index].id,
                            parentId: treeNodes[index].parentId,
                            name: treeNodes[index].name,
                            type: treeNodes[index].type
                        };
                        zNodes.push(obj);
                    }
                }
                zTreeObj = $.fn.zTree.init($("#tree"), setting, zNodes);

                var treeObj = $.fn.zTree.getZTreeObj("tree");
                var node = treeObj.getNodeByParam("id", PARENT_ID);
                if (node !== null) {
                    JJZ.named('parentId').val(node.id);
                    JJZ.named('parentName').val(node.name);
                } else {
                    JJZ.named('parentId').val(0);
                    JJZ.named('parentName').val('无');
                }

            })
        }


        form.on('submit(submit)', function (data) {
            data.field.status = data.field.status == 1 ? 1 : 0;

            if (JJZ.named('type').val() !== '0' && JJZ.named('parentId').val() === '0') {
                layer.msg('非导航栏必须要选择父节点');
                return false
            }

            if(id){
                //编辑
                JJZ.editData(tableName,data.field,function(data){
                    window.location.href =JJZ.baseUrl+ '/'+tableName+'/menuDataGrid';
                })
            }else{
                //新增
                JJZ.addData(tableName,data.field,function(data){
                    window.location.href =JJZ.baseUrl+ '/'+tableName+'/menuDataGrid';
                })
            }

            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });


    });
</script>
</body>
</html>